#!/bin/sh
#
#  SUSAN® - Sole of Unix Save ANything
#
# Copyright (C) 2000-2011 Hechzh Property
# Copyright (C) 2013-2014 Hechzh Property
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# This script dumps your Susan catalog in ASCII format
# It works for MySQL, SQLite, and PostgreSQL
#
#  $1 is the name of the database to be backed up and the name
#     of the output file (default = susan).
#  $2 is the user name with which to access the database
#     (default = susan).
#  $3 is the password with which to access the database or "" if no password
#     (default ""). WARNING!!! Passing the password via the command line is
#     insecure and should not be used since any user can display the command
#     line arguments and the environment using ps.  Please consult your
#     MySQL or PostgreSQL manual for secure methods of specifying the
#     password.
#  $4 is the host on which the database is located
#     (default "")
#  $5 is the type of database
#

#
# Source the Susan config functions.
#
. @scriptdir@/susan-config-lib.sh

working_dir=`get_working_dir`
default_db_type=`get_database_driver_default`

#
# See if the fifth argument is a valid backend name.
# If so the user overrides the default database backend.
#
if [ $# -ge 5 ]; then
   case $5 in
     sqlite3)
       db_type=$5
       ;;
     mysql)
       db_type=$5
       ;;
     postgresql)
       db_type=$5
       ;;
     *)
       ;;
   esac
fi

#
# If no new db_type is gives use the default db_type.
#
if [ -z "${db_type}" ]; then
   db_type="${default_db_type}"
fi

cd ${working_dir}
rm -f $1.sql

bindir=`get_database_utility_path ${db_type}`
if [ ! -z "${bindir}" ]; then
   PATH="$bindir:$PATH"
fi

case ${db_type} in
  sqlite3)
    echo ".dump" | sqlite3 $1.db > $1.sql
    ;;
  mysql)
    if test $# -gt 2; then
       MYSQLPASSWORD=" --password=$3"
    else
       MYSQLPASSWORD=""
    fi
    if test $# -gt 3; then
       MYSQLHOST=" --host=$4"
    else
       MYSQLHOST=""
    fi
    mysqldump -u ${2}${MYSQLPASSWORD}${MYSQLHOST} -f --opt $1 > $1.sql
    ;;
  postgresql)
    if test $# -gt 2; then
      PGPASSWORD=$3
      export PGPASSWORD
    fi
    if test $# -gt 3; then
      PGHOST=" --host=$4"
    else
      PGHOST=""
    fi
    # you could also add --compress for compression.  See man pg_dump
    pg_dump -c $PGHOST -U $2 $1 > $1.sql
    ;;
esac
#
#  To read back a MySQL database use:
#     cd @working_dir@
#     rm -f ${BINDIR}/../var/susan/*
#     mysql <susan.sql
#
#  To read back a SQLite database use:
#     cd @working_dir@
#     rm -f susan.db
#     sqlite susan.db <susan.sql
#
#  To read back a PostgreSQL database use:
#     cd @working_dir@
#     dropdb susan
#     createdb susan -T template0 -E SQL_ASCII
#     psql susan <susan.sql
#
